﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CarsModel;
using System.Data;
using System.Data.SqlClient;
using Lib.Data;
using System.Reflection;
namespace CarsDal
{
    public class UsersDal
    {
        public static string ConnectionString = "Data Source=47.93.36.227;Initial Catalog=SecondHandCars;User ID=Yang;Pwd=qaz@WSXedc";
        SqlConnection con = new SqlConnection(ConnectionString);
        /// <summary>
        /// 用户信息集合
        /// </summary>
        /// <returns></returns>
        public List<Users> LoginList()
        {
            string sql = "select * from Users";
            SqlCommand com = new SqlCommand(sql,con);
            SqlDataAdapter adapter = new SqlDataAdapter(com);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            adapter.Dispose();
            List<Users> list = DataTableToList.DataTableToLista<Users>(dt);
            return list;
            
        }

        /// <summary>
        /// 修改用户登录状态
        /// </summary>
        /// <param name="UserStateID">要修改的用户的ID</param>
        /// <returns></returns>
        public int UsersUpdateLoginState(int UserStateID,int UsersID)
        {
            string sql = "Update Users set  UsersStateID=" + UserStateID+"  where  UsersID="+UsersID;
            SqlCommand com = new SqlCommand(sql,con);
            con.Open();
            int i = com.ExecuteNonQuery();
            con.Close();
            return i;
        }
        ///// <summary>
        ///// 修改用户是卖家Or买家
        ///// </summary>
        ///// <param name="id">用户ID</param>
        ///// <returns></returns>
        //public int UsersUpdate(int id)
        //{
        //    string sql = "update Users set IsBuyOrSell=1 where UsersID="+id;
        //    SqlCommand com = new SqlCommand(sql,con);
        //    int i = com.ExecuteNonQuery();
        //    return i;
        //}


        /// <summary>
        /// 用户完善信息
        /// </summary>
        /// <param name="user">赋值之后的user实体：姓名，性别，地址，年龄</param>
        /// <returns></returns>
        public int UsersUpdateInfo(Users user)
        {
            string sql = "update Users set UsersName='"+user.UsersName+"' ,UsersSex='"+user.UsersSex+"',UsersAddress='"+user.UsersAddress+"',UsersAge= "+user.UsersAge;
            SqlCommand com = new SqlCommand(sql,con);
            con.Open();
            int i = com.ExecuteNonQuery();
            con.Close();
            return i;
        }


        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="UsersTel">用户手机号输入</param>
        /// <returns></returns>
        public int UsersRegist(string UsersTel, string UsersPwd)
        {
            string sql = "insert into Users(UsersTel,UsersPwd) values('" + UsersTel + "','" + UsersPwd + "')";
            SqlCommand com = new SqlCommand(sql,con);
            con.Open();
            int i = com.ExecuteNonQuery();
            con.Close();
            return i;
        }
    }
}
